using System;
using System.Windows;

namespace WPFUtilities
{
    public static class VisualTreeElementFinder
    {
        public static DependencyObject Find(DependencyObject root, Predicate<DependencyObject> condition)
        {
            VisualTreeIterator iterator = new DepthFirstVisualTreeIterator();
            foreach (DependencyObject dependencyObject in iterator.GetNodes(root))
            {
                if (condition(dependencyObject))
                {
                    return dependencyObject;
                }
            }

            return null;
        }

        public static DependencyObject Find(DependencyObject root, string name)
        {
            return Find(root, delegate(DependencyObject node)
                                  {
                                      return
                                          ((FrameworkElement) node).Name == name;
                                  });
        }
    }
}
